﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ClassLibrary;

namespace Forms
{
    public partial class SoldGoods : UIForm
    {
        User user;
        List<Good> goods;
        GoodService goodService = new GoodService();
        public SoldGoods(User user)
        {
            SetStyle(
                ControlStyles.UserPaint//使用自定义的绘制方式
                | ControlStyles.ResizeRedraw//当控件大小发生变化时就重新绘制
                | ControlStyles.DoubleBuffer// 双缓冲
                | ControlStyles.SupportsTransparentBackColor//则控件接受 alpha 组件数小于 255 个的 BackColor 来模拟透明度
                | ControlStyles.AllPaintingInWmPaint//禁止擦除背景.则控件忽略窗口消息 WM_ERASEBKGND 以减少闪烁
                | ControlStyles.EnableNotifyMessage// 
                                                   // Enable the OnNotifyMessage event so we get a chance to filter out 
                                                   // Windows messages before they get to the form's WndProc
                | ControlStyles.OptimizedDoubleBuffer//则控件将首先绘制到缓冲区而不是直接绘制到屏幕，这可以减少闪烁
                , true);
            InitializeComponent();
            this.user = user;
            this.goods = goodService.GetGoodsBySellerID(user.UserID);
            ListGoods(goods);
            Color userColor = MainForm.MainForm.userColor;
            UIStyles.InitColorful(userColor, Color.White);
        }
        public void ListGoods(List<Good> goods)//列表化商品
        {
            panel.Controls.Clear();
            panel.AutoScroll = true; // 启用自动滚动条
            int boxHeight = 160;
            int i = 0;
            foreach (Good good in goods)//对于每个发送者有一个消息框
            {
                Itembox box = new Itembox(user, good);
                box.FlashParentForm += Box_FlashParentForm;
                box.Top = i * boxHeight; // 设置用户控件的位置
                i++;
                panel.Controls.Add(box); // 将用户控件添加到 Panel 中
            }
        }

        private void Box_FlashParentForm()
        {
            this.goods = goodService.GetGoodsBySellerID(user.UserID);
            ListGoods(goods);
        }

        private void Search_Click(object sender, EventArgs e)
        {
            List<Good> tempGoods = new List<Good>();
            if (KeyTextBox.Text == "" || KeyTextBox.Text == null)
            {
                ListGoods(goods);
            }
            else
            {
                string key = KeyTextBox.Text;
                tempGoods = goodService.GetGoodsByUserIDandKey(user.UserID, key);
                ListGoods(tempGoods);
            }
        }
    }
}
